﻿@using Model;
@{
    SiteSecurity.RequireAuthenticatedUser();
    Layout = "~/Shared/_SiteLayout.cshtml";
    Page.Title = "Question";

    string inputType = "radio";
    string multiAnswerWarning = "";
    var statusMessage = "";
    var isValid = true;
    // post data
    bool result;
    // database
    Question aQuestion = new Question();

    aQuestion.TotalAnsweringTime = 60;

    List<QOption> selectedOptionList = null;

    List<QOption> answerOptionList = null;

    if (!IsPost)
    {
        int totalQuestionAnswerToday = SiteHelper.GetNumberOfQuestionAnsweredToday();

        if (totalQuestionAnswerToday <= WebSite.QuestionperUser)
        {
            aQuestion = SiteHelper.GetAQuestionRandomly();

            int answerCounter = 0;

            if (aQuestion != null)
            {
                selectedOptionList = SiteHelper.GetQOptionListByQuestionId(aQuestion.Id);

                answerOptionList = new List<QOption>();

                Session.Add("aQuestion", aQuestion);

                foreach (QOption eachQuestion in selectedOptionList)
                {
                    if (eachQuestion.IsAnswer == true)
                    {
                        answerCounter++;

                        answerOptionList.Add(eachQuestion);
                    }
                }

                Session.Add("answerOptionList", answerOptionList);

                if (answerCounter> 1)
                {
                    inputType = "checkbox";

                    multiAnswerWarning = "Answer can be Multiple!";
                }
            }
        }
    }
    if (IsPost)
    {
        string[] ans = { };

        var Option = "";

        answerOptionList = Session["answerOptionList"] as List<QOption>;

        int i = 1;

        selectedOptionList = new List<QOption>();

        if (answerOptionList.Count == 1)
        {
            var postAns = Request.Form["Option"];

            if (!string.IsNullOrEmpty(postAns))
            {
                QOption aQoption = new QOption();

                aQoption.Options = postAns;

                selectedOptionList.Add(aQoption);
            }
        }
        else
        {

            string postAns = Request.Form["Option"];

            if (!postAns.IsEmpty())
            {
                string[] postAnsArray = postAns.Split(new char[] { ',' }).ToArray();

                for (i = 0; i < postAnsArray.Count(); i++)
                {
                    if (!string.IsNullOrEmpty(postAnsArray[i]))
                    {
                        QOption aQoption = new QOption();

                        aQoption.Options = postAnsArray[i];

                        selectedOptionList.Add(aQoption);
                    }
                }
            }
        }

        result = SiteHelper.CheckAnswer(selectedOptionList, answerOptionList);

        aQuestion = new Question();

        if (Session["aQuestion"] != null)
        {
            aQuestion = Session["aQuestion"] as Question;
        }
      
        int totalAnsweringTimes = 0;

        if (Request.Form["totalAnsweringTime"] != null)
        {
            totalAnsweringTimes = Convert.ToInt16(Request.Form["totalAnsweringTime"]);
        }

        aQuestion.SpendingTime = aQuestion.TotalAnsweringTime - totalAnsweringTimes;

        if (result)
        {
            aQuestion.IsAnswered = 2;
            aQuestion.AnswerStatus = SiteHelper.GetStatus(aQuestion.IsAnswered);
            
            statusMessage = "Answer " + @Option + " is Right";

            isValid = true;
        }
        else
        {
            aQuestion.IsAnswered = 3;
            aQuestion.AnswerStatus = SiteHelper.GetStatus(aQuestion.IsAnswered);
            statusMessage = "Answer " + @Option + " is wrong";

            isValid = false;
        }
        SiteHelper.UpdateQuestionByIdAndObject(aQuestion);
    }
}
@* If at least one validation error exists, notify the user *@
@if (!statusMessage.IsEmpty())
{
    if (isValid)
    {
    <p class="message success">
        @statusMessage
    </p>
    }
    else
    {
    <p class="message error">
        @statusMessage
    </p>
    }
}
<form method="post" action="">
<fieldset>
    <legend>Question</legend>
    @if (!IsPost)
    {
        if (aQuestion != null)
        {
        <div class="flt_left" style="width: 400px;">
            <table>
                <tbody>
                    <tr>
                        <td class="question">
                            @aQuestion.Questions
                        </td>
                    </tr>
                    @if (!multiAnswerWarning.IsEmpty())
                    {
                        <tr>
                            <td>
                                <span class="shadow-text">(@multiAnswerWarning) </span>
                            </td>
                        </tr>
                    }
                    @for (int i = 0; i < selectedOptionList.Count; i++)
                    {
                        int increment = i + 1;

                        var idOrName = "Option" + increment;

                        <tr>
                            <td class="question-option">
                                <input type="@inputType" id="@idOrName" name="Option" value="@selectedOptionList[i].Options" title="@selectedOptionList[i].Options" />@selectedOptionList[i].Options
                            </td>
                        </tr>
                    }
                    <tr>
                        <td colspan="2">
                            <p class="form-actions">
                                <input class="button green medium" type="submit" value="Submit" title="Submit" />
                            </p>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div>
            <span class="info">Total time on hands</span>
            <br />
            <input id="totalAnsweringTime" readonly="readonly" class="no-inputbox" style="font-size: 67px; color: Red;" type="text" name="totalAnsweringTime" value="@aQuestion.TotalAnsweringTime" />
        </div>
        }
        else
        {
        <p class="message error">
            No Question available for You!
        </p>
        }
    }
    @if (!IsPost)
    {
        if (aQuestion != null)
        {
        <script type="text/javascript">
            var serverVariable = '@Question.MaxTime';

            serverVariable = '@aQuestion.TotalAnsweringTime';

            setTimeout("TimerCallBack( )", 1000);

            function TimerCallBack() {

                if (serverVariable == 0) {
                    var url = window.location;

                    window.location.replace(url);
                }

                serverVariable = serverVariable - 1;

                $('#totalAnsweringTime').val(serverVariable);

                setTimeout("TimerCallBack( )", 1000);
            }
        </script>
        }
    }
    @if (IsPost)
    {
        <div>
            <div class="message information">
                Your Answer</div>
            <ul class="question-option">
                @if (selectedOptionList.Count> 0)
                {
                    foreach (QOption option in selectedOptionList)
                    {
                    <li>@option.Options</li>
                    }
                }
                else
                {
                    <li>None</li>
                }
            </ul>
        </div>

        <div>
            <div class="message information">
                Correct Answer</div>
            <ul class="question-option">
                @foreach (QOption option in answerOptionList)
                {
                    <li>@option.Options</li>
                }
            </ul>
        </div>
                int spendingTime = (int)aQuestion.TotalAnsweringTime - (int)aQuestion.SpendingTime;

        <div class="message information">
            Answering Time: @aQuestion.SpendingTime
        </div>
        <p class="form-actions">
            <a class="button gray medium" href="@Href("~/Question")">Answer Another Questions</a>
        </p>
    }
</fieldset>
<script src="@Href("~/Scripts/jquery-1.6.2-vsdoc.js")" type="text/javascript"></script>
</form>